package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Attention;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.AttentionMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AttentionMapper attentionMapper;
    @Override
    public void register(User user) {
        User u = userMapper.selectUserByEmailAndPassword(user.getEmail(),null);
        if (u!=null) {
            throw new RuntimeException("该邮箱已被注册");
        }else{
            user.setGender(0);
            user.setCoverImgUrl("/img/user/bg.jpeg");
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setPlace("中国");
            user.setSign("这个人很懒什么都没有写");
            userMapper.insert(user);
        }
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.selectUserByEmailAndPassword(email, password);
        if (user==null) {
            throw new RuntimeException("用户名或密码错误");
        }

        return user;
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User selectByUserId(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }


}
